import pandas as pd

from assignment_5.src.scripts import get_prior_p

if __name__ == '__main__':
    data = {
        'age': ['youth', 'youth', 'middle_aged', 'senior', 'senior', 'senior', 'middle_aged', 'youth', 'youth',
                'senior',
                'youth', 'middle_aged', 'middle_aged', 'senior'],
        'income': ['high', 'high', 'high', 'medium', 'low', 'low', 'low', 'medium', 'low', 'medium', 'medium', 'medium',
                   'high', 'medium'],
        'student': ['no', 'no', 'no', 'no', 'yes', 'yes', 'yes', 'no', 'yes', 'yes', 'yes', 'no', 'yes', 'no'],
        'credit_rating': ['fair', 'excellent', 'fair', 'fair', 'fair', 'excellent', 'excellent', 'fair', 'fair', 'fair',
                          'excellent', 'excellent', 'fair', 'excellent'],
        'buys_computer': ['no', 'no', 'yes', 'yes', 'yes', 'no', 'yes', 'no', 'yes', 'yes', 'yes', 'yes', 'yes', 'no']
    }

    x = [['age', 'youth'], ['income', 'medium'], ['student', 'yes'], ['credit_rating', 'fair']]
    label = 'buys_computer'

    dataSet = pd.DataFrame(data)

    p_list = get_prior_p(x, dataSet, label)

    temp_label = ''
    temp = 0
    for a in range(len(p_list)):
        if a == 0:
            temp_label = p_list[a][0]
            temp = p_list[a][1]
        else:
            if p_list[a][1] > temp:
                temp_label = p_list[a][0]
                temp = p_list[a][1]
    print(f'对于X，朴素贝叶斯分类的类为 {label} = {temp_label}')

# import pandas as pd
# from sklearn.naive_bayes import MultinomialNB
# from sklearn.model_selection import train_test_split
# from sklearn.preprocessing import LabelEncoder
# from sklearn.metrics import accuracy_score
#
# # 创建DataFrame
# data = {
#     'age': ['youth', 'youth', 'middle_aged', 'senior', 'senior', 'senior', 'middle_aged', 'youth', 'youth', 'senior', 'youth', 'middle_aged', 'middle_aged', 'senior'],
#     'income': ['high', 'high', 'high', 'medium', 'low', 'low', 'low', 'medium', 'low', 'medium', 'medium', 'medium', 'high', 'medium'],
#     'student': ['no', 'no', 'no', 'no', 'yes', 'yes', 'yes', 'no', 'yes', 'yes', 'yes', 'no', 'yes', 'no'],
#     'credit_rating': ['fair', 'excellent', 'fair', 'fair', 'fair', 'excellent', 'excellent', 'fair', 'fair', 'fair', 'excellent', 'excellent', 'fair', 'excellent'],
#     'buys_computer': ['no', 'no', 'yes', 'yes', 'yes', 'no', 'yes', 'no', 'yes', 'yes', 'yes', 'yes', 'yes', 'no']
# }
#
# df = pd.DataFrame(data)
#
# # 使用LabelEncoder将类别数据转化为数值
# label_encoder = LabelEncoder()
# df_encoded = df.apply(label_encoder.fit_transform)
#
# # 特征与目标变量
# X = df_encoded.drop('buys_computer', axis=1)
# y = df_encoded['buys_computer']
#
# # 数据分割
# X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
#
# # 创建并训练贝叶斯分类器
# nb_classifier = MultinomialNB()
# nb_classifier.fit(X_train, y_train)
#
# # 预测并评估模型
# y_pred = nb_classifier.predict(X_test)
# accuracy = accuracy_score(y_test, y_pred)
#
# print(f"Accuracy: {accuracy:.2f}")
